﻿Imports System.ComponentModel

Public Class SuperForm
    Inherits System.Windows.Forms.Form
    Private components As System.ComponentModel.IContainer

    Private Sub InitializeComponent()
        Me.SuspendLayout()
        '
        'SuperForm
        '
        Me.BackColor = System.Drawing.Color.White
        Me.ClientSize = New System.Drawing.Size(730, 376)
        Me.Name = "SuperForm"
        Me.ResumeLayout(False)

    End Sub

    Private mSaveDimension As Boolean
    Public Property SaveDimension As Boolean
        Get
            Return mSaveDimension
        End Get
        Set(ByVal value As Boolean)
            mSaveDimension = value
        End Set
    End Property
    Private mSavePosition As Boolean
    Public Property SavePosition As Boolean
        Get
            Return mSavePosition
        End Get
        Set(ByVal value As Boolean)
            mSavePosition = value
        End Set
    End Property
    Private mNoAnimazione As Boolean
    Public Property NoAnimazione As Boolean
        Get
            Return mNoAnimazione
        End Get
        Set(ByVal value As Boolean)
            mNoAnimazione = value
        End Set
    End Property



    Dim trmShow As New System.Windows.Forms.Timer
    Dim blnApertura As Boolean = True
    Dim intLeft As Integer
    Dim intTop As Integer
    Dim DialogResultAppo As System.Windows.Forms.DialogResult
    Dim SuperMdiParent As Windows.Forms.Form = Nothing
    Dim blnModificato As Boolean = False

    Const DimensioneTopBar As Integer = 165

    Public Sub SetModificato()
        blnModificato = True
    End Sub

    Public Sub ResetModificato()
        blnModificato = False
    End Sub

    Public Function IsModificato() As Boolean
        Return blnModificato
    End Function

    Public Sub BlockClose(ByVal inBlock As Boolean)
        trmShow.Enabled = Not inBlock
    End Sub

    Protected Function IsPossibleModify() As Boolean
        Try
            If IsModificato() Then
                If MsgBox("Alcuni dati presenti sono stati modificati." & vbCrLf & "Sicuro di voler cambiare la data?" & vbCrLf & "(Le modifiche apportate andranno perse.)", vbQuestion + vbDefaultButton2 + vbYesNo, Me.Text) = MsgBoxResult.Yes Then
                    Return True
                Else
                    Return False
                End If
            Else
                Return True
            End If
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub SuperForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If DesignMode Then Return
            If blnApertura = False Then Return

            If IsModificato() Then
                If MsgBox("Alcuni dati presenti sono stati modificati." & vbCrLf & "Sicuro di voler chiudere la finestra?" & vbCrLf & "(Le modifiche apportate andranno perse.)", vbQuestion + vbDefaultButton2 + vbYesNo, Me.Text) = MsgBoxResult.No Then
                    e.Cancel = True
                    Return
                End If
            End If

            intLeft = Me.Left
            intTop = Me.Top
            If SavePosition Then
                SaveSetting("SuperForm", Me.Name & Me.Tag, "Left", intLeft)
                SaveSetting("SuperForm", Me.Name & Me.Tag, "Top", intTop)
            End If
            If SaveDimension Then
                SaveSetting("SuperForm", Me.Name & Me.Tag, "Width", Me.Width)
                SaveSetting("SuperForm", Me.Name & Me.Tag, "Height", Me.Height)
            End If

            Return
            '# da vedere un po
            If SuperMdiParent IsNot Nothing Then
                Me.MdiParent = Nothing
                Me.Left = intLeft
                Me.Top = intTop + DimensioneTopBar
            End If
            DialogResultAppo = DialogResult
            blnApertura = False
            trmShow.Enabled = True
            e.Cancel = True
            
        Catch ex As Exception
            MsgBox(ex.Message, vbCritical, "Form")
        End Try
    End Sub

    Private Sub SuperForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If DesignMode Then Return
            intLeft = GetSetting("SuperForm", Me.Name & Me.Tag, "Left", (System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2)
            intTop = GetSetting("SuperForm", Me.Name & Me.Tag, "Top", (System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2)
            Me.Top = intTop
            Me.Width = GetSetting("SuperForm", Me.Name & Me.Tag, "Width", Me.Width)
            Me.Height = GetSetting("SuperForm", Me.Name & Me.Tag, "Height", Me.Height)
            Me.Left = -Me.Width
        Catch ex As Exception
            MsgBox(ex.Message, vbCritical, "Form")
        End Try
    End Sub

    Private Sub SuperForm_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Try
            If DesignMode Then Return
            If mNoAnimazione Then Return
            blnApertura = True
            Me.Opacity = 0
            Me.Enabled = False
            trmShow.Interval = 20
            AddHandler trmShow.Tick, AddressOf trmShow_Tick
            trmShow.Enabled = True
            SuperMdiParent = Me.MdiParent
        Catch ex As Exception
            MsgBox(ex.Message, vbCritical, "Form")
        End Try
    End Sub

    Private Sub trmShow_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            If blnApertura Then
                Me.Left += 130
                Me.Opacity += 0.1
                If Me.Left >= intLeft Then
                    Me.Left = intLeft
                    Me.Opacity = 1
                    trmShow.Enabled = False
                    Me.Enabled = True
                    Me.BringToFront()
                End If
            Else
                Me.Left -= 100
                Me.Opacity -= 0.1
                If Me.Left <= -Me.Width Then
                    trmShow.Enabled = False
                    DialogResult = DialogResultAppo
                    blnApertura = False
                    Me.Close()
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, vbCritical, "Timer")
        End Try
    End Sub

End Class
